//
//  SNMYLSViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/11.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNNMPlanListViewController.h"
#import "NSDate+TimeStamp.h"
#import "SNMyArtelViewController.h"
#import "SNOrganizationViewController.h"
#import "SNMachineViewController.h"
#import "SNTechniqueViewController.h"
#import "SNNZViewController.h"
#import "HeZhuoSheDetailController.h"
#import "ProductPlanTableViewController.h"
#import "SNMYInfomationViewController.h"
#import "UIImageView+WebCache.h"
#import "ZuzhiDetail.h"
#import "ManageViewController.h"
#import "SNMYLSCollectionViewCell.h"
#import "SNMYLSViewController.h"
#import "SNSetViewController.h"
#import "Masonry.h"
#import "MyLoanTableViewController.h"
#import "UIImageView+TapGesture.h"
//组织控制器
#import "ZuZhiViewController.h"
//合作社组织控制器
#import "HeZhuoSheViewController.h"
#import "SNPlanListViewController.h"

@interface SNMYLSViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/** View */
@property (weak, nonatomic) UIImageView * bgImageView;
/** imageView */
@property (weak, nonatomic) UIImageView * iconImageView;
/** titleLabel */
@property (weak, nonatomic) UILabel * titleLabel;
/** View */
@property (weak, nonatomic) UIImageView * examineImageView;
/** View */
@property (weak, nonatomic)  UILabel * examineLabel;

/** View */
@property (weak, nonatomic) UICollectionView * collectionView;

/** titles */
@property (nonatomic,strong) NSArray<NSString *> * titles;
/** imageNames */
@property (nonatomic,strong) NSArray<NSString *> * imageNames;
@end

@implementation SNMYLSViewController
/** titles 懒加载 */
- (NSArray<NSString *> *)titles {
    if (_titles == nil) {
        NSInteger index = kUserInfo.userType.integerValue;
        switch (index) {
            case 1:
                _titles = @[@"我的组织",@"生产计划",@"我的贷款",@"我的农资",@"我的农技",@"我的农机"];
                break;
            case 2:
                _titles = @[@"我的组织",@"生产计划",@"我的贷款",@"我的农资",@"我的农技",@"我的农机"];
                break;
            case 3:
                _titles = @[@"生产计划",@"我的贷款",@"我的农资",@"我的农技",@"我的农机",@""];
                break;
            case 5:
            case 6:
                _titles = @[@"组织管理",@"贷款管理",@"农资管理",@"农技管理",@"农机管理",@""];
                break;
            default:
                break;
        }
    }
    return _titles;
}
- (NSArray<NSString *> *)imageNames {
    if (_imageNames == nil) {
        
        NSInteger index = kUserInfo.userType.integerValue;
        switch (index) {
            case 1:
                _imageNames = @[@"我的合作社",@"生产计划",@"我的贷款",@"我的农资",@"农技管理",@"我的农机"];
                break;
            case 2:
                _imageNames = @[@"我的合作社",@"生产计划",@"我的贷款",@"我的农资",@"农技管理",@"我的农机"];
                break;
            case 3:
                _imageNames = @[@"生产计划",@"我的贷款",@"我的农资",@"农技管理",@"我的农机",@""];
                break;
            case 5:
            case 6:
                _imageNames = @[@"组织管理",@"我的贷款",@"我的农资",@"农技管理",@"我的农机",@""];
                break;
            default:
                break;
        }
    }
    return _imageNames;
}
//===================================== stone ===========/
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSLog(@"kUserDetailInfo.CUSTNAME = %@",kUserDetailInfo.custName);
    
    switch (kUserInfo.userType.integerValue) {
        case 5:
        case 6:
        case 3:
        {
            NSString * custname = ([kUserInfo.userName isEqualToString:@""] || kUserInfo.userName == nil)?@"未知":kUserInfo.userName;
            NSString * usertypeName = [kUserDetailInfo.usertypename containsString:@"业务代表"] ? @"业务代表":kUserDetailInfo.usertypename;
            self.titleLabel.text = [NSString stringWithFormat:@"%@  |  %@",custname,usertypeName];
            break;
        }
        case 1:
        case 2:
        
        {
            NSString * custname = ([kUserInfo.userName isEqualToString:@""] || kUserInfo.userName == nil)?@"未知":kUserInfo.userName;
            NSString * usertypeName = [kUserDetailInfo.usertypename containsString:@"业务代表"] ? @"业务代表":kUserDetailInfo.usertypename;
            
            self.titleLabel.text = [NSString stringWithFormat:@"%@  |  %@  |  %@",custname,usertypeName,kUserDetailInfo.fullName];
            break;
        }
        default:
            break;
    }
    
    [SNUtils specialLabelWithstring:self.titleLabel.text keyString:@"|" label:self.titleLabel color:HexRGB(0xf5f5f5)];
    
    
//    NSURL * url = [NSURL URLWithString:kUserDetailInfo.photo];
//    [self.iconImageView sd_setImageWithURL:url placeholderImage:kUserIconDefaultImage];
    //TODO: 得做缓存 不然每次来都得请求头像
    if (kUserDetailInfo.photoImage) {
        NSLog(@"kUserDetailInfo.photoImage = %@",kUserDetailInfo.photoImage);
        self.iconImageView.image = kUserDetailInfo.photoImage;
    }else{
        self.iconImageView.image = kUserIconDefaultImage;
    }
}
- (void)receivedMyHeaderIconNotification:(NSNotification *)notify
{
    if (kUserDetailInfo.photoImage) {
        NSLog(@"kUserDetailInfo.photoImage = %@",kUserDetailInfo.photoImage);
        self.iconImageView.image = kUserDetailInfo.photoImage;
    }else{
        self.iconImageView.image = kUserIconDefaultImage;
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)addObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedMyHeaderIconNotification:   ) name:@"MyHeaderIconNotification" object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = HexRGB(0xf5f5f5);
    self.navigationItem.title = @"我的卓信";
    // Do any additional setup after loading the view.
    [self addViews];
    [self setupUI];
    
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc] init];
    /** 垂直方向 间距 */
    flowLayout.minimumLineSpacing = 1;
    /** 横向方向 间距 */
    flowLayout.minimumInteritemSpacing = 1;
    /** 内缩 内容 */
    flowLayout.sectionInset = UIEdgeInsetsMake(12, 12, 12, 12);
    
    NSInteger colNum = 3;
    
    CGFloat w =  ([UIScreen mainScreen].bounds.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right - flowLayout.minimumInteritemSpacing * (colNum - 1)) / colNum;
    
    /** item大小 */
    flowLayout.itemSize = CGSizeMake(w,103);
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.bgImageView.frame), [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - CGRectGetMaxY(self.bgImageView.frame)-49) collectionViewLayout:flowLayout];
        
        self.collectionView = collectionView;
        /** 分页处理 */
        // collectionView.pagingEnabled = YES;
        
        collectionView.backgroundColor = HexRGB(0xf5f5f5);
        collectionView.dataSource = self;
        collectionView.delegate = self;
        
        [collectionView registerClass:[SNMYLSCollectionViewCell class] forCellWithReuseIdentifier:@"SNMYLSCollectionViewCell"];
        [self.view addSubview:collectionView];
        
    });
    
    
    [self.navigationController.navigationBar setBackgroundImage:[SNUtils imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[SNUtils imageWithColor:[UIColor clearColor]]];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"设置"] style:UIBarButtonItemStyleDone target:self action:@selector(rightBtnClcik)];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    [self.navigationController.navigationBar setTintColor:HexRGB(0x818181)];
}
-(void)rightBtnClcik{
    
    //    UIViewController *setSb = [[UIStoryboard storyboardWithName:@"ZXSetTableViewController" bundle:nil] instantiateInitialViewController];
    UIViewController * setSb = [[SNSetViewController alloc] init];
    //
    [self.navigationController pushViewController:setSb animated:YES];
    
}
- (void)addViews
{
    UIImageView * bgImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
        //        imageView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
        //        UIImage * image = [UIImage imageNamed:@"01-sili"];
        //        imageView.image = image;
        
        // [self addSubview:imageView];
        UIImage * bgImage = [UIImage imageNamed:@"bgimage"];
        //        imageView.backgroundColor = [UIColor colorWithPatternImage:bgImage];
        imageView.image = bgImage;
        [self.view addSubview:imageView];
        
        imageView;
    });
    self.bgImageView = bgImageView;
    
    UIImageView * iconImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
        //        imageView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1.0];
        UIImage * image = [UIImage imageNamed:@"01-sili"];
        imageView.image = image;
        dispatch_async(dispatch_get_main_queue(), ^{
            
            imageView.layer.cornerRadius = imageView.bounds.size.width * 0.5;
            imageView.layer.masksToBounds = YES;
        });
        // [self addSubview:imageView];
        [self.view addSubview:imageView];
        
        @weakify_self
        imageView.sn_tapBlock = ^(){
            @strongify_self
            SNMYInfomationViewController *vc = [[SNMYInfomationViewController alloc]init];
            //            vc.number = @"农场";
            [self.navigationController pushViewController:vc animated:YES];
            
        };
        
        imageView;
    });
    self.iconImageView = iconImageView;
    
    
    
    UILabel * titleLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 1;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"李达  |  业务代表  |  卓信";
        label.font = [UIFont systemFontOfSize:15];
        
        [SNUtils specialLabelWithstring:label.text keyString:@"|" label:label color:HexRGB(0xf5f5f5)];
        
        // [self addSubview:label];
        [self.view addSubview:label];
        
        @weakify_self
        label.sn_tapBlock = ^(){
            @strongify_self
            SNMYInfomationViewController *vc = [[SNMYInfomationViewController alloc]init];
            //            vc.number = @"农场";
            [self.navigationController pushViewController:vc animated:YES];
            
        };
        
        label;
    });
    self.titleLabel = titleLabel;
    
    
    UIImageView * examineImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
        //        imageView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:1.0];
        UIImage * image = [UIImage imageNamed:@"pass"];
        imageView.image = image;
        
        // [self addSubview:imageView];
        [self.view addSubview:imageView];
        
        imageView;
    });
    self.examineImageView = examineImageView;
    
    UILabel * examineLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = @"已审核";
        label.font = [UIFont systemFontOfSize:10];
        
        // [self addSubview:label];
        [examineImageView addSubview:label];
        
        label;
    });
    self.examineLabel = examineLabel;
    
    examineImageView.hidden = YES;
}
- (void)setupUI
{
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).offset(0);
        make.top.mas_equalTo(self.view.mas_top).offset(67);
        
        make.width.mas_equalTo(75);
        make.height.mas_equalTo(75);
        
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        
        // make.centerX.mas_equalTo(self.view.mas_centerX).offset(0);
        make.left.mas_equalTo(self.view.mas_left).offset(15);
        make.right.mas_equalTo(self.view.mas_right).offset(-15);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(23);
        
    }];
    
    [self.examineImageView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).offset(0);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(5);
        
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(15);
        
    }];
    
    
    [self.examineLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.examineImageView.mas_left).offset(15);
        make.centerY.mas_equalTo(self.examineImageView.mas_centerY).offset(0);
        make.width.mas_equalTo(45);
    }];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        //13
        make.bottom.mas_equalTo(self.examineImageView.mas_bottom).offset(13);
        
    }];
}
//sn_note:=========  ============================ stone 🐳 ===========/
#pragma mark - <UICollectionViewDataSource>
/** section 数量 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
/** item 数量 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.titles.count;
}
/** UICollectionViewCell */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    SNMYLSCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SNMYLSCollectionViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    NSLog(@"self.titles = %@",self.titles);
    NSLog(@"self.titles[indexPath.item] = %@",self.titles[indexPath.item]);
    [cell configWithTitle:self.titles[indexPath.item] imageName:self.imageNames[indexPath.item]];
    
    return cell;
}

#pragma mark - <UICollectionViewDelegate>
// 选中操作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@ | %s",self.class,__func__);
    SNMYLSCollectionViewCell * cell = (SNMYLSCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    NSString * title = cell.titleLabel.text;
    
    if ([title isEqualToString:@"组织管理"]) {
        
        switch (kUserInfo.userType.integerValue) {
            case 5:
            case 6:
            {
                //                ZuZhiViewController * zzvc = [[ZuZhiViewController alloc] init];
                SNOrganizationViewController *zzvc = [[SNOrganizationViewController alloc]init];
                [self.navigationController pushViewController:zzvc animated:YES];
            }
                break;
            case 7:
            {
                HeZhuoSheViewController *sylbVC = [[HeZhuoSheViewController alloc]init];
                [self.navigationController pushViewController:sylbVC animated:YES];
            }
                break;
            default:
                break;
        }
        
        
    }else if ([title isEqualToString:@"贷款管理"] ||[title isEqualToString:@"我的贷款"]) {
        //业务代表 - 贷款管理
        MyLoanTableViewController *MC = [[MyLoanTableViewController alloc]init];
        MC.title = title;
        [self.navigationController pushViewController:MC animated:YES];
    }
    else if ([title isEqualToString:@"农资管理"]) {
        SNNZViewController *vc = [[SNNZViewController alloc]init];
        vc.titleText = @"农资管理";
        [self.navigationController pushViewController:vc animated:YES];
    }else if([title isEqualToString:@"我的农资"]){
        SNNZViewController *vc = [[SNNZViewController alloc]init];
        vc.titleText = @"我的农资";
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([title isEqualToString:@"农技管理"]) {
        SNTechniqueViewController *vc = [[SNTechniqueViewController alloc]init];
        vc.titleText = @"农技管理";
        [self.navigationController pushViewController:vc animated:YES];
    }else if([title isEqualToString:@"我的农技"]){
        SNTechniqueViewController *vc = [[SNTechniqueViewController alloc]init];
        vc.titleText = @"我的农技";
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([title isEqualToString:@"农机管理"]) {
        SNMachineViewController *vc = [[SNMachineViewController alloc]init];
        vc.titleText = @"农机管理";
        [self.navigationController pushViewController:vc animated:YES];
    }else if([title isEqualToString:@"我的农机"]){
        SNMachineViewController *vc = [[SNMachineViewController alloc]init];
        vc.titleText = @"我的农机";
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if ([title isEqualToString:@"生产计划"]) {
        SNNMPlanListViewController *vc = [[SNNMPlanListViewController alloc]init];
        vc.empty = NO;
        SNPlantingDecisionMakerResult * plantingDecisionMakerResult = [SNPlantingDecisionMakerResult new];
        plantingDecisionMakerResult.photo = kUserDetailInfo.photo;
        plantingDecisionMakerResult.custName = kUserDetailInfo.custName;
        plantingDecisionMakerResult.custNo = kUserDetailInfo.custNo;
        plantingDecisionMakerResult.fullName = kUserDetailInfo.fullName;
        
        
        vc.plantingDecisionMakerResult = plantingDecisionMakerResult;
        vc.year = [NSString stringWithFormat:@"%@年度生产计划",[NSDate new].dateToString(@"yyyy")];
        
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([title isEqualToString:@"我的组织"]) {
        //        HeZhuoSheDetailController *hzsVc = [[UIStoryboard storyboardWithName:@"HeZhuoSheDetailController" bundle:nil] instantiateInitialViewController];
        //        [self.navigationController pushViewController:hzsVc animated:YES];
        SNMyArtelViewController *vc = [[SNMyArtelViewController alloc]init];
        vc.userId = kUserInfo.userId;
        vc.companyNo = kUserInfo.companyNo;
        
        NSLog(@"%@",kUserInfo.userId);
        NSLog(@"%@",kUserInfo.companyNo);
        
        [self.navigationController pushViewController:vc animated:YES];
        
    }else{
        NSLog(@"title = %@",title);
    }
    
}
// 取消选中操作
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%@ | %s",self.class,__func__);
}

@end
